package com.diandian.backend.douyin.life.exchange.service;

import com.alibaba.fastjson.JSON;
import com.diandian.backend.douyin.life.exchange.client.DianDianStoreClient;
import com.diandian.backend.douyin.life.exchange.client.dto.StoreDouyinLifeStoreInfoDetailParamDTO;
import com.diandian.backend.douyin.life.exchange.client.dto.StoreDouyinLifeStoreInfoDetailResp;
import com.diandian.base.boot.starter.exception.DefaultException;
import com.diandian.base.boot.starter.response.BaseResult;
import com.diandian.douyin.life.boot.starter.dto.request.CertificatePrepareReq;
import com.diandian.douyin.life.boot.starter.dto.request.CertificateVerificationPrepareReq;
import com.diandian.douyin.life.boot.starter.dto.request.CertificateVerificationReq;
import com.diandian.douyin.life.boot.starter.dto.request.CertificateVerifyReq;
import com.diandian.douyin.life.boot.starter.dto.response.CertificateVerificationPrepareResponse;
import com.diandian.douyin.life.boot.starter.dto.response.CertificateVerificationResponse;
import com.diandian.douyin.life.boot.starter.service.product.DouyinLifeProductService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DouyinLifeCertificateBusinessService {

    private static final Logger logger = LoggerFactory.getLogger(DouyinLifeCertificateBusinessService.class);

    @Autowired
    private DouyinLifeProductService douyinLifeProductService;

    @Autowired
    private DianDianStoreClient dianDianStoreClient;

    public CertificateVerificationPrepareResponse prepare(String belong, String token, String account, CertificatePrepareReq data){

        logger.info("执行验券准备参数：{} | {} | {} | {}", belong, token, account, JSON.toJSONString(data));

        // 获取请求参数
        String offlineCode = data.getPoi_id();
        if (offlineCode == null || "".equals(offlineCode)) {
            throw new RuntimeException("线下门店编码不能为空");
        }
        String encrypted_data = data.getEncrypted_data();
        String code = data.getCode();

        // 从门店中心获取线上门店编码
        String storeOnlineCode = offlineStoreCodeToOnlineStoreCode(belong, offlineCode);
        logger.info("线下门店{}兑换到线上门店编码{}", offlineCode, storeOnlineCode);

        // 执行验签
        CertificateVerificationPrepareReq prepareReq = new CertificateVerificationPrepareReq();
        prepareReq.setPoi_id(storeOnlineCode);
        prepareReq.setEncrypted_data(encrypted_data);
        prepareReq.setCode(code);
        CertificateVerificationPrepareResponse prepareResponse = douyinLifeProductService.certificateVerificationPrepare(token, account, prepareReq);
        logger.info("验券准备结果：{}", JSON.toJSONString(prepareResponse));

        return prepareResponse;
    }

    public CertificateVerificationResponse verification(String belong, String token, CertificateVerifyReq data){

        logger.info("执行验券参数：{} | {} | {}", belong, token, JSON.toJSONString(data));

        // 获取参数
        String offlineCode = data.getPoi_id();
        if (offlineCode == null || "".equals(offlineCode)) {
            throw new RuntimeException("线下门店编码不能为空");
        }

        // 从门店中心获取线上门店编码
        String storeOnlineCode = offlineStoreCodeToOnlineStoreCode(belong, offlineCode);
        logger.info("线下门店{}兑换到线上门店编码{}", offlineCode, storeOnlineCode);

        // 组装请求对象
        CertificateVerificationReq param  = new CertificateVerificationReq();
        param.setPoi_id(storeOnlineCode);
        param.setEncrypted_codes(data.getEncrypted_codes());
        param.setVerify_token(data.getVerify_token());
        CertificateVerificationResponse verifyResponse = douyinLifeProductService.certificateVerification(token, param);
        logger.info("验券结果：{}", JSON.toJSONString(verifyResponse));

        // 执行请求
        return verifyResponse;
    }

    /**
     * 使用线下门店编码兑换线上门店编码
     * @param offlineStoreCode
     * @return
     */
    private String offlineStoreCodeToOnlineStoreCode(String belong, String offlineStoreCode){

        StoreDouyinLifeStoreInfoDetailParamDTO detailParamDTO = new StoreDouyinLifeStoreInfoDetailParamDTO();
        detailParamDTO.setOfflineCode(offlineStoreCode);
        detailParamDTO.setBelong(belong);
        BaseResult baseResult = dianDianStoreClient.getDouyinStoreDetail(detailParamDTO);
        if(baseResult == null || baseResult.getCode() != 0){
            throw new DefaultException("获取抖音门店信息异常");
        }
        if(baseResult.getData() == null){
            throw new DefaultException("未获取到抖音门店");
        }

        StoreDouyinLifeStoreInfoDetailResp storeDetail = JSON.parseObject(JSON.toJSONString(baseResult.getData()), StoreDouyinLifeStoreInfoDetailResp.class);
        if(storeDetail == null || storeDetail.getOnlineCode() == null || "".equals(storeDetail.getOnlineCode())){
            throw new DefaultException("未获取到抖音门店");
        }

        return storeDetail.getOnlineCode();
    }

}
